<?php
require_once '../libs/app.config.php';
session_start();
if($_SESSION['LoginId'] == false){
	header("Location:login.php");
}

 //排序
 if($_GET['order'] and !($id==$_SESSION['cat_id']and$sort_order==$_SESSION['cat_sort_order'])){
	$updown = $_GET['order'];
	$sort_order = $_GET['sort_order'];
	$id = $_GET['id'];
	$updown=='up'?$op='<':$op='>';
	$updown=='up'?$order='desc':$order='asc';
	$_SESSION['cat_id']=$id;
	$_SESSION['cat_sort_order']=$sort_order;
	$sql = sprintf('select acs.id,acs.sort_order from article_cat acs, article_cat act where acs.sort_order %s %d and acs.level=%d and acs.partent_id=act.partent_id and act.id=%d order by sort_order %s limit 1',$op,$sort_order,$level,$id,$order);
//echo $sql;exit();
	$query = $tpm->query($sql);
	if($tpm->num_rows($query)){
		while($row = $tpm->fetch_array($query)){
			$a = array('sort_order'=>$row['sort_order']);
			$w = sprintf('id=%d',$id);
			$tpm->update('article_cat',$a,$w);
			$a = array('sort_order'=>$sort_order);
			$w = sprintf('id=%d',$row['id']);
			$tpm->update('article_cat',$a,$w);
			$_GET['sort_order']=$row['sort_order'];
		}
	}
 }
//无限分类列表
function mainfl($i)
{ global $shows;
	$result=mysql_query("select id,partent_id,cat_name,sort_order,level from article_cat where partent_id=0 and( cat_name='CARGO SERVICES' or cat_name = 'EXPRESS SERVICES') order by sort_order");
	if ($myrow=mysql_fetch_array($result))
	{
	  do{
	  $shows.=" <tr >
      <td width='16%' height='30' bgcolor='#f2f2f2' class='left_txt' style='padding-left:10px;'>
      <input type='checkbox' id='checks' name='check[]' value='".$myrow['id']."'>$i</td>
      <td width='45%' height='30' class='left_txt' bgcolor='#f2f2f2'>
<b>".$myrow['cat_name']."</b></td><td width='16%' height='30' class='left_txt' bgcolor='#f2f2f2'>
		 <img style='cursor:pointer' class='upli' src='images/do_top.gif' onClick='javascript:location.href=\"news_catlist.php?level={$myrow['level']}&order=up&id={$myrow['id']}&sort_order={$myrow['sort_order']}\";' title='Move Up'/>
		 <img style='cursor:pointer' src='images/do_bottom.gif' onClick='javascript:location.href=\"news_catlist.php?level={$myrow['level']}&order=down&id={$myrow['id']}&sort_order={$myrow['sort_order']}\";' title='Move Down'/>
		</td>
      <td width='23%' height='30' bgcolor='#f2f2f2' class='left_txt'></td>
              </tr>";
		 $i++;
		 $i=subfl($myrow["id"],"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",$i);
		}
	  while ($myrow=mysql_fetch_array($result));
	}
}

function subfl($fid,$tag,$i)
{   global $shows;
	$result1=mysql_query("select id,partent_id,cat_name,sort_order,level from article_cat where partent_id=$fid  order by sort_order asc");
	if ($myrow1=mysql_fetch_array($result1))
	{
		do {

	 $shows.=" <tr >
      <td width='16%' height='30' bgcolor='#f2f2f2' class='left_txt' style='padding-left:10px;'>
      <input type='checkbox' id='checks' name='check[]' value='".$myrow1['id']."'>$i</td>
      <td width='45%' height='30' class='left_txt' bgcolor='#f2f2f2'>
 ".$tag.$myrow1['cat_name']."</td><td width='16%' height='30' class='left_txt' bgcolor='#f2f2f2'>
		 <img style='cursor:pointer' class='upli' src='images/do_top.gif' onClick='javascript:location.href=\"news_catlist.php?level={$myrow1['level']}&order=up&id={$myrow1['id']}&sort_order={$myrow1['sort_order']}\";' title='Move Up'/>
		 <img style='cursor:pointer' src='images/do_bottom.gif' onClick='javascript:location.href=\"news_catlist.php?level={$myrow1['level']}&order=down&id={$myrow1['id']}&sort_order={$myrow1['sort_order']}\";' title='Move Down'/>
		</td>
      <td width='23%' height='30' bgcolor='#f2f2f2' class='left_txt'><a href='news_catadd.php?id=".$myrow1['id']."'><img src='images/icon_edit.gif' border='0'></a>&nbsp;<a style='cursor: pointer;' onClick='del(".$myrow1['id'].")'><img src='images/icon_trash.gif' border='0'></a></td>
              </tr>";
            $i++;
			$i=subfl($myrow1["id"],"&nbsp;&nbsp;".$tag,$i);
			}
		while ($myrow1=mysql_fetch_array($result1));
	}
	return $i;
}

mainfl(1);


//多项删除
if($_POST['submit'])
{
	if(!empty($_POST['check']))
	{
		$array=$_POST['check'];
		$arr=implode(',',$array);
		$id=explode(',',$arr);
		$size=count($id);
		for($j=0;$j<$size;$j++)
		{
			$sql="delete from article_cat where id='".$id[$j]."'";
			$query=$tpm->query($sql);
			if($query>0)
			echo "<script>alert('Delete successfully!');history.back();</script>";
		}
	}
}

//单项删除
if($_POST['delid'])
{
	$sql="delete from article_cat where id='".$_POST['delid']."'";
	$query=$tpm->query($sql);
	if($query>0)
	{
		$sql="delete from articles where cat_id='".$_POST['delid']."'";
		$querys=$tpm->query($sql);
		if($querys>0)
		echo "1";else echo "0";
	}

}
require_once(INCLUDE_PATH.'template.inc.php');  //模板类及对象$tpl的加载
    $tpl->assign("shows",$shows);
	$tpl->display("news_catlist.html");
?>